using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace Report.PhieuA
{
    public partial class UcA19 : DevExpress.XtraEditors.XtraUserControl
    {
        public UcA19()
        {
            InitializeComponent();
        }

        private void UcA19_Load(object sender, EventArgs e)
        {
            DataTable dtA19 = new DsReportX.Table4ColDataTable();
            dtA19 = CalculateData(dtA19);
            gridControlA.DataSource = dtA19;
        }

        DataTable CalculateData(DataTable dt)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            SqlCommand cmd = con.CreateCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                con.Open();
                cmd.CommandText = "DECLARE @TongNumber1 INT \n" +
                                "DECLARE @TongNumber2 INT  \n" +
                                "SELECT @TongNumber1 = SUM(Number1), @TongNumber2 = SUM(Number2)  \n" +
                                "FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID \n" +
                                "WHERE LoaiPhieu = 'PhieuA' AND MaCau = 17 \n" +
                                "SELECT SUM(Number1) Number1, CONVERT(MONEY, 100.0 * SUM(Number1)/@TongNumber1) Number2, \n" +
                                "	   SUM(Number2) Number3, CONVERT(MONEY, 100.0 * SUM(Number2)/@TongNumber2) Number4 \n" +
                                "FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID \n" +
                                "WHERE LoaiPhieu = 'PhieuA' AND MaCau = 17 \n" +
                                "GROUP BY ThuTuTrongBang ";

                da.SelectCommand = cmd;
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
